#ifndef MYOPENGLWIDGET_H
#define MYOPENGLWIDGET_H

#include <QDebug>
#include <QDir>
#include <QMouseEvent>
#include <QOpenGLBuffer>
#include <QOpenGLFunctions_3_3_Core>
#include <QOpenGLShaderProgram>
#include <QOpenGLVertexArrayObject>
#include <QOpenGLWidget>
#include <QTimer>
#include <QWidget>
#include <QThread>
#include "tiny_obj_loader.h"


class MyOpenGLWidget : public QOpenGLWidget, QOpenGLFunctions_3_3_Core
{
    Q_OBJECT
public:
    explicit MyOpenGLWidget(QWidget *parent = nullptr);

protected:
    virtual void initializeGL();
    virtual void resizeGL(int w, int h);
    virtual void paintGL();

private:
    QTimer m_tm;
    std::vector<QVector3D> normals;

private:
    tinyobj::attrib_t attrib;
    std::vector<tinyobj::shape_t> shapes;
    std::vector<tinyobj::material_t> materials;

    QOpenGLShaderProgram *shaderProgram;
    QOpenGLVertexArrayObject *vAO;
    QOpenGLBuffer *vBO;
    QOpenGLBuffer *eBO;
    int nCount;

private:
    // control
    QVector3D destinationAngles;
    QVector3D destinationScales;
    QVector3D destinationViewPos;

public:
    void Run3DModel();

signals:

public slots:
    void slotTimeout();
    void controlMessage(QVector3D angles, QVector3D scale, QVector3D viewPos);

    /**
     * @brief The OpenGLWidgetRunThread class
     * 运行OpenGL 3D 模型的子线程，内部类
     */

public:
    class OpenGLWidgetRunThread : public QThread {
    public:
        explicit OpenGLWidgetRunThread(QObject *parent = nullptr){
        }

    private:
        MyOpenGLWidget*         m_pMyOpenGLWidget;
        bool m_isRunning = false;

    public:
        void Stop(){
            m_isRunning = false;
        }

        void SetMyOpenGLWidget(MyOpenGLWidget* pWidget){
            m_pMyOpenGLWidget = pWidget;
        }

    public:
        void run() override {
            // 在这里执行你的线程任务
            m_isRunning = true;

            while (m_isRunning) {
                QThread::msleep(10);
                m_pMyOpenGLWidget->Run3DModel();
            }
        }
    };
};

#endif // MYOPENGLWIDGET_H
